package org.lql.algo.codecrush.week008;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: liangqinglong
 * @date: 2025-08-19 00:00
 * @description: 面试题 16.02. 单词频率 <a href="https://leetcode.cn/problems/words-frequency-lcci/description/">...</a>
 **/
public class WordsFrequency {

	private Map<String, Integer> freqMap;

	public WordsFrequency(String[] book) {
		freqMap = new HashMap<>();
		for (String s : book) {
			freqMap.put(s, freqMap.getOrDefault(s, 0) + 1);
		}
	}

	public int get(String word) {
		return freqMap.getOrDefault(word, 0);
	}

	public static void main(String[] args) {
		String[] book = {"i", "have", "an", "apple", "he", "have", "a", "pen"};
		WordsFrequency wordsFrequency = new WordsFrequency(book);
		System.out.println(wordsFrequency.get("have"));
		System.out.println(wordsFrequency.get("an"));
		System.out.println(wordsFrequency.get("apple"));
		System.out.println(wordsFrequency.get("pen"));
	}
}
